package com.huatai.vba.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huatai.vba.common.utils.FileUtils;
import com.huatai.vba.common.utils.ListUtils; 

@Service
public class FileSaveServiceImpl implements FileSaveService {

	public String disc = "D";

	@Autowired
	private SaveFileDAO saveFileDAO;

	@Override
	public void saveFile(String currectDate) throws Exception {
		String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
		if (!currectDate.equals(yyyyMMdd))
			return;
		String yymm = new SimpleDateFormat("yyMM").format(new Date());
		String seatmpFilePath = disc + ":/pro_data/prg/seatmap";
		createSeatmap(seatmpFilePath);

		// cope 文件
		String year = yyyyMMdd.substring(0, 4);
		String mouthAndDay = yyyyMMdd.substring(4, 8);
		String fileDir = yyyyMMdd.substring(2, 8);
		File dst = new File(disc + ":/remote/dbf/" + year + "/" + mouthAndDay + "/" + fileDir + "/");
		File dst1 = new File(disc + ":/szhq/" + year + "/" + mouthAndDay + "/" + fileDir + "/");
		File dst2 = new File(disc + ":/vsat3/" + year + "/" + mouthAndDay + "/" + fileDir + "/");
		// 20161011 深交所新接口行情文件,复制到深圳行情文件夹里
		File dst3 = new File(disc + ":/szhq/" + yymm + "/" + yyyyMMdd.substring(2, 8) + "/");
		copyShow(dst);
		copyDbfFile(dst);
		copyMsgFile(dst);
		copyGzlx(dst);
		copyDstStr1(dst1);
		copyDstStr2(dst2);
		copyDst3(dst3);
	}

	private void createSeatmap(String seatmpFilePath) {
		try {
			File seatmpFile = new File(seatmpFilePath);
			if (seatmpFile.exists()) {
				seatmpFile.delete();
			}
			List<Map> list = saveFileDAO.queryList();
			List<String> contents = new ArrayList<String>();
			System.out.println("======" + list.size() + " ==========");
			if (ListUtils.isNotEmpty(list)) {
				System.out.println("======start ==========");
				for (Map m : list) {
					StringBuffer content = new StringBuffer();
					System.out.println(content.toString());
					content.append(m.get("vc_fund_code"));
					content.append(",");
					content.append(m.get("vc_fund_name"));
					content.append(",");
					content.append(m.get("vc_seat_id"));
					contents.add(content.toString());
				}
				System.out.println("======end ==========");
				FileUtils.writeLines(seatmpFile, contents);
			}
			System.out.println("=====生成seatmp文件成功====");
		} catch (Exception ex) {
			System.out.println("=====生成seatmp文件失败====");
			ex.printStackTrace();
		}

	}

	private void copyDst3(File dst) {
		try {
			String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
			String strPathSource = "H:/szhq/szhqnew/";
			File[] files3 = FileUtils.findFiles(new File(strPathSource), "*" + yyyyMMdd + "*");
			FileUtils.copy(files3, dst);
			System.out.println("=====复制Dst3文件成功====，文件个数为：" + files3.length);
		} catch (Exception ex) {
			System.out.println("=====复制DstStr2文件失败====");
			ex.printStackTrace();
		}
	}

	private void copyDstStr2(File dst) {
		try {
			FileUtils.copy(new File("h:/vsat3/LOFJS.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSDZ.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSGF.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSHB.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSQS.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSTJ.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSWT.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSZJ.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSGB.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSJG.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSJX.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSKS.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSQS0.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSTP.dbf"), dst);
			FileUtils.copy(new File("h:/vsat3/SJSYE.dbf"), dst);

			// 从dcom复制过来
			String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			String strPathSource = "h:/dcom/" + date + "/";
			String mmdd = new SimpleDateFormat("MMdd").format(new Date());

			FileUtils.copy(new File(strPathSource + "SJSMX0" + mmdd + ".dbf"), dst);
			FileUtils.copy(new File(strPathSource + "SJSMX1" + mmdd + ".dbf"), dst);
			FileUtils.copy(new File(strPathSource + "SJSMX2" + mmdd + ".dbf"), dst);
			System.out.println("=====复制DstStr2文件成功====");
		} catch (Exception ex) {
			System.out.println("=====复制DstStr2文件失败====");
			ex.printStackTrace();
		}
	}

	private void copyDstStr1(File dst) {
		try {
			FileUtils.copy(new File("h:/szhq/sjshq.dbf"), dst);
			System.out.println("=====复制sjshq.dbf文件成功====");
			FileUtils.copy(new File("h:/szhq/sjsxx.dbf"), dst);
			System.out.println("=====复制sjsxx.dbf文件成功====");
			FileUtils.copy(new File("h:/szhq/sjsxxn.dbf"), dst);
			System.out.println("=====复制sjsxxn.dbf文件成功====");
			FileUtils.copy(new File("h:/szhq/sjszs.dbf"), dst);
			System.out.println("=====复制sjszs.dbf文件成功====");
		} catch (Exception ex) {
			System.out.println("=====复制DstStr2文件失败====");
			ex.printStackTrace();
		}
	}

	private void copyShow(File dst) {
		try {
			FileUtils.copy(new File("h:/remote/dbf/show2003.dbf"), dst);
			System.out.println("=====复制show2003.dbf文件成功====");
		} catch (Exception ex) {
			System.out.println("=====复制show2003.dbf文件失败====");
			ex.printStackTrace();
		}

	}

	private void copyDbfFile(File dst) {
		String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
		File fld = new File("h:/remote/dbf/");

		File[] files1 = FileUtils.findFiles(fld, "gh*.dbf");
		File[] files2 = FileUtils.findFiles(fld, "bgh*.dbf");
		File[] files3 = FileUtils.findFiles(fld, "ipogh*.txt");
		// 上交所fast行情
		// **************************************************************************************
		File[] files4 = FileUtils.findFiles(fld, "mktdt*.txt");
		File[] files5 = FileUtils.findFiles(fld, "fjy*" + date + "*.txt");
		File[] files6 = FileUtils.findFiles(fld, "cpxx*.txt");
		// 固定收益平台
		File[] files7 = FileUtils.findFiles(fld, "zgh040*.dbf");
		try {
			FileUtils.copy(files1, dst);
			FileUtils.copy(files2, dst);
			FileUtils.copy(files3, dst);
			FileUtils.copy(files4, dst);
			FileUtils.copy(files5, dst);
			FileUtils.copy(files6, dst);
			FileUtils.copy(files7, dst);
			int num = files1.length + files2.length + files3.length + files4.length + files5.length + files6.length
					+ files7.length;
			System.out.println("复制文件成功，总共复制了文件个数为" + num);
		} catch (Exception ex) {
			System.out.println("=====复制DstStr2文件失败====");
			ex.printStackTrace();
		}
	}

	private void copyMsgFile(File dst) {
		String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
		String mouthAndDay = date.substring(4, 8);
		File fld = new File("h:/remote/msg/");
		File[] files1 = FileUtils.findFiles(fld, "trdses*.txt");
		File[] files2 = FileUtils.findFiles(fld, "exra*" + mouthAndDay + "txt");
		File[] files3 = FileUtils.findFiles(fld, "reff*" + mouthAndDay + "txt");
		File[] files4 = FileUtils.findFiles(fld, "zxjc*" + mouthAndDay + "txt");
		try {
			FileUtils.copy(files1, dst);
			FileUtils.copy(files2, dst);
			FileUtils.copy(files3, dst);
			FileUtils.copy(files4, dst);
			System.out.println("复制文件成功，总共复制了文件个数为" + (files1.length + files2.length + files3.length + files4.length));
		} catch (Exception ex) {
			System.out.println("=====复制DstStr2文件失败====");
			ex.printStackTrace();
		}

	}

	private void copyGzlx(File dst) {
		String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
		String strMonth = date.substring(4, 6).replace("0", "");
		String strDay = date.substring(6, 8);
		if ("12".equals(strMonth))
			strMonth = "c";
		else if ("11".equals(strMonth))
			strMonth = "b";
		else if ("10".equals(strMonth))
			strMonth = "a";
		String fileName = "GZLX." + strMonth + strDay;
		try {
			FileUtils.copy(new File("h:/remote/msg/" + fileName), dst);
		} catch (Exception ex) {
			System.out.println("=====复制h:/remote/msg/" + fileName + "文件失败====");
			ex.printStackTrace();
		}
	}

}
